package com.vadmin.vadminjava.servlet;

import com.alibaba.fastjson.JSONObject;
import com.vadmin.vadminjava.service.WxCouponAdminService;
import com.vadmin.vadminjava.service.WxCouponBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/* ***********************************************************
 *
 * 微信的卡劵接口，后台专用
 *
 * 文档：https://developers.weixin.qq.com/doc/offiaccount/Cards_and_Offer/Create_a_Coupon_Voucher_or_Card.html
 * 申请沙箱测试账号：https://developers.weixin.qq.com/doc/offiaccount/Cards_and_Offer/WeChat_Coupon_Interface.html#1
 *
 * **********************************************************/
@RestController
@RequestMapping("/WxCouponAdminServlet")
public class WxCouponAdminServlet {

    @Autowired
    WxCouponBusinessService wxCouponBusinessService;

    @Autowired
    WxCouponAdminService wxCouponAdminService;



    //创建商家券
    @RequestMapping(value = {"/createV3CouponBusiness"})
    public String createV3Coupon(@RequestParam Map<String, Object> params) throws Exception {
        JSONObject paramJ = new JSONObject();
        params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
        JSONObject v3Coupon = wxCouponBusinessService.createV3Coupon(paramJ);
        return v3Coupon.toJSONString();
    }


    //卡券详情（用于核销的，塞入了其它东西）
    @RequestMapping(value = {"/couponDetailForUse"})
    public Map<String, Object> couponDetailForUse(@RequestParam Map<String, Object> params) throws Exception {
        JSONObject paramJ = new JSONObject();
        params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
        return wxCouponAdminService.couponDetailForUse(paramJ);
    }

    //商家券核销
    @RequestMapping(value = {"/useCouponBusiness"})
    public Map<String, Object> useCouponBusinessAdmin(@RequestParam Map<String, Object> params) throws Exception {
        JSONObject paramJ = new JSONObject();
        params.keySet().forEach(i -> paramJ.put(i, params.get(i)));

        //paramJ.put("openid", "oqGpx6BpNgk_Fftn7Xa1-2-WNSok");
        //paramJ.put("coupon_id", "1210840000000011");
        //paramJ.put("coupon_code", "1214781515007750890569");

        return wxCouponAdminService.useCouponBusinessAdminDispatch(paramJ);
    }


    //商家券列表
    @RequestMapping (value = {"/couponListOfBusiness"})
    public String wxWorkExternalUserList(@RequestParam Map<String, Object> params,HttpServletRequest httpServletRequest) {
        JSONObject paramJ = new JSONObject();
        params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
        return wxCouponAdminService.wxCouponBusinessList(paramJ).toString();
    }


    //客户已领取的商家券列表
    @RequestMapping (value = {"/couponListOfBusinessFetched"})
    public String couponListOfBusinessFetched(@RequestParam Map<String, Object> params,HttpServletRequest httpServletRequest) {
        JSONObject paramJ = new JSONObject();
        params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
        return wxCouponAdminService.couponListOfBusinessFetched(paramJ).toString();
    }

    //从微信服务器读取当前商家券回调url
    @RequestMapping (value = {"/getCallbackUrlOfCouponBusiness"})
    public String getCallbackUrlOfCouponBusiness(@RequestParam Map<String, Object> params,HttpServletRequest httpServletRequest) {
        JSONObject paramJ = new JSONObject();
        params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
        return wxCouponAdminService.getCallbackUrlOfCouponBusiness(paramJ).toString();
    }


    //设置商家券回调url
    @RequestMapping (value = {"/setCallbackUrlOfCouponBusines"})
    public String setCallbackUrlOfCouponBusines(@RequestParam Map<String, Object> params,HttpServletRequest httpServletRequest) {
        JSONObject paramJ = new JSONObject();
        params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
        return wxCouponAdminService.setCallbackUrlOfCouponBusines(paramJ).toString();
    }



}
